package com.mebs.model.logistics;

import com.mebs.model.inventory.OrderItem;
import com.mebs.model.inventory.BoxSubItem;

import javax.persistence.*;

import java.util.HashSet;
import java.util.Set;

import org.jt.jet.util.JetUtils;

/**
 * Created by Sergey Prasolov, sap@jensentechnologies.com Date: Mar 6, 2006, 7:51:28 PM
 */
@Entity()
@Table(name = "STRUCTURED_BOX")
@Inheritance(strategy = InheritanceType.JOINED)
@PrimaryKeyJoinColumn(name = "Id")
public class StructuredBox extends Box {
    @OneToMany(fetch = FetchType.LAZY)
    @JoinColumn(name = "BoxId")
    @OrderBy("orderItem")
    private Set<BoxItem> items;

    /**
     * Constructors
     */

    public StructuredBox()
    {
    }

    public StructuredBox(
        Air air,
        Integer number,
        Double dimL,
        Double dimW,
        Double dimH,
        Integer dimUnit,
	 	Double weight,
        Integer weightUnit)
    {
        super(number, dimL, dimW, dimH, dimUnit/*, null, null*/, weight, weightUnit);
        super.setAir(air);
    }

    public StructuredBox(
        OceanContainer container,
        Integer number,
        Double dimL,
        Double dimW,
        Double dimH,
        Integer dimUnit,
	 	Double weight,
        Integer weightUnit)
    {
        super(number, dimL, dimW, dimH, dimUnit/*, null, null*/, weight, weightUnit);
        super.setOceanContainer(container);
    }

    public StructuredBox(
        Truck truck,
        Integer number,
        Double dimL,
        Double dimW,
        Double dimH,
        Integer dimUnit,
	 	Double weight,
        Integer weightUnit)
    {
        super(number, dimL, dimW, dimH, dimUnit/*, null, null*/, weight, weightUnit);
        super.setTruck(truck);
    }

    public StructuredBox(
        Lot lot,
        Integer number,
        Double dimL,
        Double dimW,
        Double dimH,
        Integer dimUnit)
    {
        super(number, dimL, dimW, dimH, dimUnit/*, null, null*/, null, null);
        super.setLot(lot);
    }

    public StructuredBox(
        Integer number,
        Double dimL,
        Double dimW,
        Double dimH,
        Integer dimUnit,
//        Double volume,
//        Integer volumeUnit,
        Double weight,
        Integer weightUnit)
    {
        super(number, dimL, dimW, dimH, dimUnit, /*volume, volumeUnit,*/ weight, weightUnit);
    }

    /**
     * ********************** Getters-setters ***********************
     */

    public Set<BoxItem> getItems() {
    	//will prevent NullPointerExceptions in many places
    	if (items == null) {
    		items = new HashSet<BoxItem>();
    	}
        return items;
    }

    public void setItems(Set<BoxItem> items)
    {
        this.items = items;
    }

    @Transient
    public boolean isDeletable()
    {
        return JetUtils.isEmpty(this.items);
    }

    @Transient
    public Integer getItemQuantity(OrderItem orderItem)
    {
        if (items == null || orderItem == null) return 0;

        for (BoxItem bi : items) {
            if ((bi.getOrderItem() != null)
                && bi.getOrderItem().equals(orderItem)
                && JetUtils.isEmpty(bi.getSubItem())) {
                return bi.getQuantity();
            }
        }

        return 0;
    }

    @Transient
    public Integer getItemQuantity(BoxSubItem subItem)
    {
        if (items == null || subItem == null) return 0;

        for (BoxItem bi : items) {
            if (bi.getSubItem() != null && bi.getSubItem().equals(subItem)) {
                return bi.getQuantity();
            }
        }

        return 0;
    }

    @Transient
    public BoxItem getBoxItem(OrderItem orderItem)
    {
        if (items == null || orderItem == null) return null;

        for (BoxItem bi : items) {
            if (bi.getOrderItem().equals(orderItem)) {
                return bi;
            }
        }

        return null;
    }

    @Transient
    public BoxItem getBoxItem(BoxSubItem subItem)
    {
        if (items == null || subItem == null) return null;

        for (BoxItem bi : items) {
            if (bi.getSubItem() != null && bi.getSubItem().equals(subItem)) {
                return bi;
            }
        }

        return null;
    }
}
